package com.bg.rpc.utils;

import org.dom4j.*;
import com.thoughtworks.xstream.XStream;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * XML工具类
 */
public class XmlUtil {

    private final static String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

    private XmlUtil() {
    }

    //返回数据的list
    private static List<Map<String, String>> handleList = new ArrayList<>();

    /**
     * 功能描述: 序列化XML
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/29 15:15
     */
    public static <T> String toXML(Object obj) {
        XStream stream = getXStream();
        stream.processAnnotations(obj.getClass());
        return new StringBuffer(XML_DECLARATION).append(stream.toXML(obj)).toString();
    }

    /**
     * 功能描述: 反序列化XML
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/29 15:15
     */
    public static <T> T fromXML(String xmlStr, Class<T> clazz) {
        XStream stream = getXStream();
        stream.processAnnotations(clazz);
        Object obj = stream.fromXML(xmlStr);
        try {
            return clazz.cast(obj);
        } catch (ClassCastException e) {
            return null;
        }
    }

    /**
     * 功能描述: 获取指定节点的值
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/29 15:15
     */
    public static String getNodeValue(String xpath, String dataStr) {
        try {
            // 将字符串转为xml
            Document document = DocumentHelper.parseText(dataStr);
            // 查找节点
            Element element = (Element) document.selectSingleNode(xpath);
            if (element != null) {
                return element.getStringValue();
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 功能描述: 获取Xstream实例
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/29 15:16
     */
    public static XStream getXStream() {
        return new XStream();
    }

    public static void main(String[] args) {
        readXml("rpc-service-register.xml").forEach(m -> System.out.println(m.get("interface")));
    }

    /**
     * 功能描述: 解析xml
     *
     * @param:
     * @return:
     * @auther: bonree
     * @date: 2018/8/29 15:37
     */
    public static List<Map<String, String>> readXml(String path) {
        SAXReader saxReader = new SAXReader();
        try {
            //根据类加载文件
            InputStream in = XmlUtil.class.getClassLoader().getResourceAsStream(path);
            Document document = saxReader.read(in);
            Element ele = document.getRootElement();
            parserNode(ele);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return handleList;
    }

    /**
     * 递归遍历所有父节点、子节点
     *
     * @param ele
     */
    private static void parserNode(Element ele) {
        //获取父节点中的属性值
        List<Attribute> attrList = ele.attributes();
        Map<String, String> handleMap = new ConcurrentHashMap<>();
        for (Attribute attr : attrList) {
            //每循环一次，解析此节点的一个[属性=值]，没有则输出空
            String name = attr.getName();
            String value = attr.getValue();
            handleMap.put(name, value);
//            System.out.println(name + "=" + value);
        }
        if (handleMap != null && handleMap.size() > 0) {
            handleList.add(handleMap);
        }

        List<Element> eleList = ele.elements();
        //递归遍历父节点下的所有子节点
        for (Element e : eleList) {
            parserNode(e);
        }
    }
}
